package org.example.service;

import jakarta.annotation.Resource;
import org.example.bean.Users;
import org.example.dao.UserDao;
import org.example.dto.UserDto;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class UserService {
    @Resource UserDao userDao;
    public UserDto findByUsername(String account) {
        Users users = userDao.loadByAccount(account);
        // 1. 合并角色权限和资源权限
        Stream<String> s1 = users.getPermissionList().stream()
                .map(perm -> perm.getPerm());      // 资源权限（如 "user:view"）
        Stream<String> s2 = users.getRoleList().stream().map(role -> "role:" + role.getRoleCode());      // 角色权限（如 "role:admin"）
        List<String>authorities = Stream.concat(s1,s2)
                .filter(au -> au != null)
                .collect(Collectors.toList());
        // 2. 构建UserDto（包含权限列表）
        return UserDto.builder()
                .username(users.getUserName())
                .account(users.getAccount())
                .password(users.getPassword())
                .authorities(authorities)
                .build();
    }
}
